package com.otitan.la.forest.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.otitan.la.forest.system.dto.MenuDTO;
import com.otitan.la.forest.system.entity.MenuEntity;
import com.otitan.la.forest.system.service.MenuService;
import com.otitan.la.forest.system.vo.MenuTreeVO;
import com.otitan.webapp.framework.basepro.model.DataResult;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * 菜单表
 *
 * @author jiacs
 * @email xxx
 * @date 2020-09-15 10:34:19
 */
@Slf4j
@Api(tags = "菜单表API")
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;


    @ApiOperation(value = "新增")
    @PostMapping()
    public DataResult add(@Valid @RequestBody MenuEntity menu, BindingResult bindingResult) {
        return menuService.add(menu, bindingResult);
    }

    @ApiOperation(value = "批量删除")
    @DeleteMapping("/batchDelete")
    public DataResult batchDelete(@RequestBody @ApiParam(value = "ID集合") List<String> ids) {
        return menuService.batchDelete(ids);
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("{id}")
    public DataResult delete(@PathVariable("id") String id) {
        List<String> ids = new ArrayList<String>() {{
            add(id);
        }};
        return menuService.batchDelete(ids);
    }

    @ApiOperation(value = "更新")
    @PutMapping
    public DataResult editor(@Valid @RequestBody MenuEntity menu, BindingResult bindingResult) {
        return menuService.editor(menu, bindingResult);
    }

    @ApiOperation(value = "列表查询（分页）")
    @PostMapping("/{page}/{size}")
    public DataResult<IPage<MenuEntity>> findPageData(@PathVariable("page") long page,
                                                      @PathVariable("size") long size,
                                                      @RequestBody MenuDTO menu) {
        return menuService.findPageData(page, size, menu);
    }

    @ApiOperation(value = "根据ID查询")
    @GetMapping("/{id}")
    public DataResult<MenuEntity> findOne(@PathVariable("id") String id) {
        return menuService.findOne(id);
    }

    /**
     * 获取tree型菜单
     *
     * @param [appCode]
     * @return com.otitan.webapp.framework.basepro.model.DataResult
     * @throws
     * @author Sunke
     * @date 2020/9/18 0018 11:08
     */
    @ApiOperation(value = "获取tree型菜单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "appCode", required = true, value = "应用编码"),
            @ApiImplicitParam(name = "name", required = false, value = "菜单名称"),
            @ApiImplicitParam(name = "roleId", required = false, value = "角色ID"),
    })
    @GetMapping("getTreeMenuList")
    public DataResult<List<MenuTreeVO>> getTreeMenuList(String appCode, String name, String roleId) {
        List<MenuTreeVO> list = menuService.getTreeMenuList(appCode, name, roleId);
        return DataResult.success(list);
    }

}
